<template>
  <div class="list">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <div style="margin-top: 15px;">
      <el-input placeholder="请输入内容"
                v-model="goodsObj.query"
                class="input-with-select"
                style="width:300px; margin-right: 15px"
                @keyup.enter.native="init"
                >
        <el-button slot="append"
                   icon="el-icon-search" @click="init" ></el-button>
      </el-input>
      <el-button type="primary"
    @click="$router.push({name:'add'})"
                 >添加商品</el-button>
    </div>
    <!-- 表格区域 -->
    <el-table :data="goodsList"
              border
              style=" width: 100% margin-top: 10px;">
      <el-table-column label="编号"
                       type="index"
                       >
      </el-table-column>
      <el-table-column prop="goods_name"
                       label="商品名称"
                     width="800px"
                      >
      </el-table-column>
      <el-table-column prop="goods_price"
                       label="商品价格"
                      >
      </el-table-column>
      <el-table-column prop="goods_state"
                       label="商品状态"
                       >
      </el-table-column>
      <el-table-column prop="goods_weight"
                       label="商品重量"
                       >
      </el-table-column>
      <el-table-column prop="add_time"
                       label="创建时间"
                       >
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="">
          <el-tooltip class="item"
                      effect="dark"
                      content="编辑"
                      placement="top">

            <el-button type="primary"
                       icon="el-icon-edit"></el-button>
          </el-tooltip>
          <el-tooltip class="item"
                      effect="dark"
                      content="分配角色"
                      placement="top">
            <el-button type="primary"
                       icon="el-icon-share"></el-button>
          </el-tooltip>
          <el-tooltip class="item"
                      effect="dark"
                      content="删除"
                      placement="top">
            <el-button type="primary"
                       icon="el-icon-delete">
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页结构 -->
      <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="goodsObj.pagenum"
      :page-sizes="[10,20,30,40]"
      :page-size="goodsObj.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="goodsObj.total">
    </el-pagination>
  </div>
  </div>
</template>

<script>
import { getGoodsAll } from '@/api/goods.js'
export default {
  data () {
    return {
      goodsList: [],
      goodsObj: {
        query: '',
        pagenum: 1,
        pagesize: 10,
        total: 0
      }
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    // 请求数据
    init () {
      getGoodsAll(this.goodsObj)
        .then((res) => {
          console.log(res)
          if (res.data.meta.status === 200) {
            this.goodsList = res.data.data.goods
            this.goodsObj.total = res.data.data.total
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.goodsObj.pagesize = val
      this.init()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.goodsObj.pagenum = val
      this.init()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
